-- create the module's table
local dumpn120_72 = {}

-- import required modules
local dict = require "scripts.app.dict"
local nes = require "scripts.app.nes"
local dump = require "scripts.app.dump"
local flash = require "scripts.app.flash"
local time = require "scripts.app.time"
local files = require "scripts.app.files"
local swim = require "scripts.app.swim"
local buffers = require "scripts.app.buffers"


-- local functions
local function create_header( file, prgKB, chrKB )
	-- Mapper : 227 ; Mirroring : 0 (Horizontal)

	nes.write_header( file, prgKB, chrKB, 227, 0)
end

--dump the PRG ROM
local function dump_prgrom( file, rom_size_KB, debug )

	--PRG-ROM dump 16KB at a time
	local KB_per_read = 16
	local num_reads = rom_size_KB / KB_per_read
	local read_count = 0
	local addr_base = 0x80	-- $8000

print("CPU dump :")

	while ( read_count < num_reads )
	do
		if debug then print( "dump PRG part ", read_count, " of ", num_reads) end

		-- pause of 1 second to take care about the chips
		local time=os.clock()+1
		while time>os.clock()
			do
			end

		dict.nes("NES_CPU_WR", 0x8000 | ((read_count & 0x1f) << 2) | ((read_count << 3) & (1 << 8)) , read_count)

		dump.dumptofile( file, KB_per_read, addr_base, "NESCPU_PAGE", false )

		read_count = read_count + 1
	end
end

--Cart should be in reset state upon calling this function
--this function processes all user requests for this specific board/mapper
--local function process( test, read, erase, program, verify, dumpfile, flashfile, verifyfile)
local function process(process_opts, console_opts)
	local test = process_opts["test"]
	local read = process_opts["read"]
	local erase = process_opts["erase"]
	local program = process_opts["program"]
	local verify = process_opts["verify"]
	local dumpfile = process_opts["dump_filename"]
	local flashfile = process_opts["flash_filename"]
	local verifyfile = process_opts["verify_filename"]

	local rv = nil
	local file
	local prg_size = console_opts["prg_rom_size_kb"]
	local chr_size = console_opts["chr_rom_size_kb"]
	local wram_size = console_opts["wram_size_kb"]

--initialize device i/o for NES
	dict.io("IO_RESET")
	dict.io("NES_INIT")

print("\nRunning dumpN120_72.lua")

--dump the cart to dumpfile
	if read then
		print("\nDumping ROM...")

		file = assert(io.open(dumpfile, "wb"))

		--create header: pass open & empty file & rom sizes
		create_header(file, prg_size, chr_size)

		--TODO find bank table to avoid bus conflicts!
		--dump cart into file
		time.start()

		--dump cart into file
		dump_prgrom(file, prg_size, false)

		time.report(prg_size)

		--close file
		assert(file:close())
		print("DONE Dumping ROM")
	end

	dict.io("IO_RESET")
end

-- functions other modules are able to call
dumpn120_72.process = process

-- return the module's table
return dumpn120_72
